﻿using System;
using System.Data;
using System.Configuration; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; 
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;
using mail = System.Net.Mail;

public class Mailer
{
    private string _MailFrom;
    private string _MailTo;
    private string _MailSubject;
    private string _MailBody;
    private string _SMTPServer;
    private int _SMTPPort;
    private string _SMTPUsername;
    private string _SMTPPassword;
    private bool _SMTPSSL;
    private mail.MailMessage MailObject;
    private string _MailCC;

    #region Public Properties
    public string MailFrom
    {
        set { _MailFrom = value; }
        get { return _MailFrom; }
    }
    public string MailTo
    {
        set { _MailTo = value; }
        get { return _MailTo; }
    }
    public string MailCC
    {
        set { _MailCC = value; }
        get { return _MailCC; }
    }
    public string MailSubject
    {
        set { _MailSubject = value; }
        get { return _MailSubject; }
    }
    public string MailBody
    {
        set { _MailBody = value; }
        get { return _MailBody; }
    }
    public string SMTPServer
    {
        set { _SMTPServer = value; }
        get { return _SMTPServer; }
    }
    public int SMTPPort
    {
        set { _SMTPPort = value; }
        get { return _SMTPPort; }
    }
    public string SMTPUsername
    {
        set { _SMTPUsername = value; }
        get { return _SMTPUsername; }
    }
    public string SMTPPassword
    {
        set { _SMTPPassword = value; }
        get { return _SMTPPassword; }
    }
    public bool SMTPSSL
    {
        set { _SMTPSSL = value; }
        get { return _SMTPSSL; }
    }

   
    #endregion

    #region Public Function

    public bool Send()
    {
        mail.MailMessage Email = new mail.MailMessage();
        mail.MailAddress MailFrom = new mail.MailAddress(_MailFrom, _MailFrom);
        Email.From = MailFrom;
        Email.To.Add(_MailTo);
        Email.Subject = _MailSubject;
        Email.IsBodyHtml = true;
        Email.Body = _MailBody;
        mail.SmtpClient SmtpMail = new mail.SmtpClient(_SMTPServer, _SMTPPort);
        SmtpMail.Credentials = new System.Net.NetworkCredential(_SMTPUsername, _SMTPPassword);
        SmtpMail.EnableSsl = _SMTPSSL;

        bool bTemp = true;
         

        //try
        //{
        //    SmtpMail.Send(Email);
        //    return true;
        //}
        //catch (mail.SmtpFailedRecipientsException ex)
        //{
        //    bTemp = false;
        //}


        LoadDataDelegate delLoadData = new LoadDataDelegate(SendAsyncMail);
        delLoadData.BeginInvoke(SmtpMail, Email, this.LoadComplete, delLoadData);
        return bTemp;
    }
    void SendAsyncMail(SmtpClient client, MailMessage msg)
    {
        try
        {
            client.Send(msg);
        }
        catch (Exception) { }
    }


    delegate void LoadDataDelegate(SmtpClient client, MailMessage msg);


    private void LoadComplete(IAsyncResult ar)
    {
        LoadDataDelegate d = (LoadDataDelegate)ar.AsyncState;
        //end the Load method call
        d.EndInvoke(ar);

    }

    public bool Send(string PdfPath)
    {
        /*----------  Build the email message-----------*/

        mail.MailMessage Email = new mail.MailMessage();
        mail.MailAddress MailFrom = new mail.MailAddress(_MailFrom, _MailFrom);
        Email.From = MailFrom;
        Email.To.Add(_MailTo);
        Email.Subject = _MailSubject;
        Email.IsBodyHtml = true;
        Email.Body = _MailBody;
        mail.SmtpClient SmtpMail = new mail.SmtpClient(_SMTPServer, _SMTPPort);
        SmtpMail.Credentials = new System.Net.NetworkCredential(_SMTPUsername, _SMTPPassword);
        SmtpMail.EnableSsl = _SMTPSSL;

        bool bTemp = true;

        string attachmentPath = PdfPath;
        Attachment inline = new Attachment(attachmentPath);
        inline.ContentDisposition.Inline = true;
        inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
        inline.ContentType.MediaType = "application/pdf";
        inline.ContentType.Name = Path.GetFileName(attachmentPath);
        Email.Attachments.Add(inline);
        try
        {
            SmtpMail.Send(Email);
            return true;
        }
        catch (mail.SmtpFailedRecipientsException ex)
        {
            bTemp = false;
        }
        return bTemp;
    }

    #endregion
}
